const cloud = require('wx-server-sdk');

cloud.init();

exports.main = async (event, context) => {
    const fun_name = event.fun_name;
    const collectionName = event.collection_name;
    const params = event.params;
    switch (fun_name) {
        case 'delete_all_video':
            return delete_all_video(params, collectionName);
        case 'delete_id_video':
            return delete_id_video(collectionName, params);
        default:
            return {
                errMsg: '云函数没有此方法'
            }
    }
}

async function delete_all_video (paramsList, collection_name) {
    try {
        return cloud.database().collection(collection_name).where({
            open_id: paramsList.open_id,
        }).remove()
    } catch (error) {
        cloud.logger().error({
            error
        });
        return error;
    }
}

async function delete_id_video (collection_name, paramsList) {
    try {
        return cloud.database().collection(collection_name).where({
            open_id: paramsList.open_id,
            video_id: paramsList.id
        }).remove()
    } catch (error) {
        cloud.logger().error({
            error
        });
        return error;
    }
}